#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 lizongzhe 
#
# Distributed under terms of the MIT license.
from esr_parser.models import KeywordTask, ProductListTask
from rest_framework import viewsets
from esr_parser.serializer import KeywordTaskSerializer, ProductListTaskSerializer
from rest_framework.views import APIView
from rest_framework.decorators import detail_route, list_route
from rest_framework.response import Response
from esr_parser import shortcuts

class KeywordTaskViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = KeywordTask.objects.select_related().all()
    serializer_class = KeywordTaskSerializer

    filter_fields = ('keyword',)


class ProductListTaskViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows groups to be viewed or edited.
    """
    queryset = ProductListTask.objects.select_related().all()
    serializer_class = ProductListTaskSerializer

    filter_fields = ('name',)
    
    @list_route(methods=['get'])
    def product_detail(self, request):
        print type(request)
        print dir(request)
        pid = request.query_params.get('pid', None)
        size = int(request.query_params.get('size', 0))
        is_tm = int(request.query_params.get('is_tm'))
        assert pid, 'query params pid not found'
        data = shortcuts.item_detail(pid, is_tm, size)

        return Response(data)
     



